package com.bestcem.xm.survey.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.qdes.grpc.v1.services.Project;
import com.bestcem.xm.survey.entity.SurveyExtParameter;
import com.bestcem.xm.survey.grpc.client.QdesGrpcClient;
import com.bestcem.xm.survey.mapper.SurveyExtParameterMapper;
import com.bestcem.xm.survey.service.SurveyExtParameterService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author guita
 * @description 针对表【ds_survey_extparameter】的数据库操作Service实现
 * @createDate 2023-01-09 15:15:37
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SurveyExtParameterServiceImpl extends XmServiceImpl<SurveyExtParameterMapper, SurveyExtParameter>
        implements SurveyExtParameterService {
    private @NonNull QdesGrpcClient qdesGrpcClient;

    @Override
    public SurveyExtParameter getExtparameters(String projectId, JSONObject args) {
        String version = args.getString("version");
        return getExtParam(projectId, version, Boolean.TRUE);
    }

    @Override
    public SurveyExtParameter getExtParam(String projectId, String version, Boolean upsert) {
        if (upsert == null) {
            upsert = Boolean.FALSE;
        }
        if (ObjectUtil.isEmpty(version)) {
            Project project = qdesGrpcClient.getProject(projectId);
            version = String.valueOf(project.getReleaseVer());
        }
        LambdaQueryWrapper<SurveyExtParameter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SurveyExtParameter::getVersion, version)
                .eq(SurveyExtParameter::getProjectId, projectId);
        SurveyExtParameter surveyExtParameter = baseMapper.selectOne(queryWrapper);

        if (ObjectUtil.isEmpty(surveyExtParameter)) {
            LambdaQueryWrapper<SurveyExtParameter> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SurveyExtParameter::getProjectId, projectId)
                    .orderByDesc(SurveyExtParameter::getVersion)
                    .last(" limit 1");
            surveyExtParameter = baseMapper.selectOne(queryWrapper1);
        }
        if (ObjectUtil.isEmpty(surveyExtParameter) && upsert) {
            String aesKey = RandomStringUtils.randomAscii(16);

            queryWrapper.clear();
            queryWrapper.eq(SurveyExtParameter::getProjectId, projectId)
                    .eq(SurveyExtParameter::getVersion, version);
            surveyExtParameter = baseMapper.selectOne(queryWrapper);
            if (ObjectUtil.isEmpty(surveyExtParameter)) {
                surveyExtParameter = new SurveyExtParameter();
                surveyExtParameter.setProjectId(projectId);
                surveyExtParameter.setVersion(Integer.valueOf(version));
                surveyExtParameter.setData("[]");
                surveyExtParameter.setAesKey(aesKey);
                surveyExtParameter.setSecret("");
                baseMapper.insert(surveyExtParameter);
            } else {
                surveyExtParameter.setAesKey(aesKey);
                surveyExtParameter.setUpdatedDt(LocalDateTime.now());
                baseMapper.updateById(surveyExtParameter);
            }
        }

        return surveyExtParameter;
    }
}




